package com.logstep;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity implements StepCountListener {
    private boolean isStart=false;
    private DrawView drawView;
    private TextView tv_step;
    private int stepCount=0;
    private TextView tv_angle;
    private TextView tv_accelerate;
    private TextView tv_magnetic;

    float allStep=0;
    private TextView tv_location;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        StepMeter stepMeter = new StepMeter();
        stepMeter.initListener(this);
        sensorManager.registerListener(stepMeter, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
        sensorManager.registerListener(stepMeter, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
        drawView=findViewById(R.id.sv);

        tv_step=findViewById(R.id.tv_step);
        tv_angle=findViewById(R.id.tv_angle);
        tv_accelerate=findViewById(R.id.accelerate);
        tv_magnetic=findViewById(R.id.magnetic);
        tv_location=findViewById(R.id.tv_location);
    }
    @SuppressLint("SetTextI18n")
    @Override
    public void countStep(float stepLength) {
        if (isStart){
            drawView.autoAddPoint(50, new Location() {
                @Override
                public void location(float x, float y) {
                    tv_location.setText("当前位置:("+x+","+y+")");
                }
            });
            stepCount++;
            allStep+=stepLength;
//        非线性步长估算
            tv_step.setText("步数:"+ stepCount+"      总共走了:"+String.valueOf(allStep)+"米");
        }
    }
    @SuppressLint("SetTextI18n")
    @Override
    public void direction(int v) {
        drawView.autoDrawArrow(v);
        tv_angle.setText("角度:"+ v);
    }
    public void start(View view) {
        isStart=true;
    }
    public void stop(View view) {
        isStart=false;
    }
    public void clear(View view) {
        drawView.clear();
        stepCount=0;
        allStep=0;
        tv_step.setText(String.valueOf(stepCount));
    }
    @SuppressLint("SetTextI18n")
    public void sensorValue(SensorEvent event){
        if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
            tv_accelerate.setText("加速度:"+event.values[0]+","+event.values[1]+","+event.values[2]);
        }
        if (event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){
            tv_magnetic.setText("地磁场:"+event.values[0]+","+event.values[1]+","+event.values[2]);
        }
    }
}
